﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace AppTouchScreen.Converters
{
    /// <summary>
    /// Provide converting Windows.Visibility to bool and back
    /// </summary>
    [ValueConversion(typeof(Visibility), typeof(bool))]
    public class VisibilityToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool result = false;
            if (value is Visibility)
            {
                var visibility = (Visibility)value;
                if (visibility == Visibility.Visible)
                {
                    result = true;
                }
            }
            else
            {
                throw new Exception("It's not Windows.Visibility enum");
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Hidden;
            if (value is bool)
            {
                var b = (bool)value;
                if (b)
                {
                    result = Visibility.Visible;
                }
            }
            else
            {
                throw new Exception("It's not bool type");
            }

            return result;
        }
    }
}
